package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IConsultSessionService;
import com.naiterui.ehp.bs.doctor.vo.DialogueDisplayVO;
import com.naiterui.ehp.bs.doctor.vo.SessionParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 医生端聊天会话controller
 *
 * @since 2.0.0
 */
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.GRAPHIC_CONSULT})
@Validated
@RequestMapping("ad/consult")
@Controller
public class AdConsultSessionController {

    /**
     * 日志对象：<code>logger</code>
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(AdConsultSessionController.class);

    @Autowired
    private IConsultSessionService consultSessionService;

    /**
     * 开启会话(IM端调用)
     * ConsultSessionController.openSession()
     *
     * @param paramVO 会话参数vo
     *
     * @return
     *
     * @Date 2016年2月27日
     * @since 2.0.0
     */
    @ApiOperation(value = "开启会话(IM端调用)",httpMethod = "POST")
    @RequestMapping("openSession")
    @ResponseBody
    //TODO 非inner前缀内部调用接口
    public Map<String, Object> openSession(SessionParamVO paramVO) throws BusinessException {
        if (!paramVO.validate()) {
            LOGGER.error(" consult openSession ERROR ; illegal param : sessionParamVO {}", paramVO);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        // cost、costType
        return this.consultSessionService.addNewSession(paramVO);
    }

    /**
     * 结束回话（IM调用）
     * ConsultSessionController.closeSession()
     *
     * @param sessionId IM会话标识
     * @param endTime   结束时间
     * @param freeFlag  是否免单(0：免单,1：不免单)
     * @param isManual  是否手动关闭
     *
     * @return
     *
     * @Date 2016年2月27日
     * @since 2.0.0
     */
    @ApiOperation(value = "结束回话(IM端调用)",httpMethod = "POST")
    @RequestMapping("closeSession")
    @ResponseBody
    //TODO 非inner前缀内部调用接口
    public Map<String, Object> closeSession(String sessionId, Long endTime, Integer freeFlag, Boolean isManual,
                                            Boolean isDeletePatient) throws BusinessException {
        LOGGER.info(" consult closeSession INFO : sessionId {},endTime {},freeFlag {}", sessionId, endTime, freeFlag);

        if (StringUtils.isEmpty(sessionId) || null == endTime || null == freeFlag) {
            LOGGER.error(" consult closeSession ERROR ; illegal param : sessionId {},endTime {},freeFlag {}", sessionId, endTime,
                         freeFlag);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        // payStatus

        return this.consultSessionService.updateSessionForClose(sessionId, endTime, freeFlag, isManual, isDeletePatient);
    }

    /**
     * 对话框是否展示病历、处方和结束按钮
     * @param doctorId
     * @param patientId
     * @param inquirerId
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "对话框是否展示病历、处方和结束按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者ID", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "inquirerId", value = "就诊人ID", dataType = "Long", paramType = "query"),
    })
    @GetMapping("dialogueDisplay")
    @ResponseBody
    public DialogueDisplayVO dialogueDisplay(@NotNull(message = "医生ID不能为空") Long doctorId, @NotNull(message = "患者ID不能为空") Long patientId, Long inquirerId) throws BusinessException {
        LOGGER.info("dialogue display INFO : doctorId {}, patientId {}, inquirerId {}", doctorId, patientId, inquirerId);
        if (null == doctorId || null == patientId) {
            LOGGER.error("dialogue display ERROR ; illegal param :  doctorId {}, patientId {}", doctorId, patientId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return this.consultSessionService.dialogueDisplay(doctorId, patientId, inquirerId);
    }

}
